import { ConnectorDataSource, DataSource, ValueType, columnRename, query, queryForMap } from "@/services";


import { EditableTableColumn } from '@/components/table';


export const sqliteColumns = async (): Promise<EditableTableColumn[]> => {
    //获取到表结构
    const columns: EditableTableColumn[] = [
        {
            name: 'cid',
            valueType: ValueType.Number,
            readOnly: true,
        },
        {
            name: 'name',
            valueType: ValueType.String,
        },
        {
            name: 'type',
            valueType: ValueType.String,
        },
        {
            name: 'notnull',
            valueType: ValueType.String,
        },
        {
            name: 'Default',
            valueType: ValueType.String,
        },
        {
            name: 'pk',
            valueType: ValueType.String,
        }
    ];
    return columns;
};


export const sqliteTableRequest = async (dataSource: ConnectorDataSource, table: string): Promise<Array<any>> => {
    let data = await queryForMap(dataSource, `PRAGMA table_info(${table})`, [], {})
    console.log(data);
    return data;
};

export const sqliteAlterTable = async (dataSource: ConnectorDataSource, table: string, rowIndex: number, columnId: string, value: unknown, data: { [key: string]: any }) => {
    if (columnId === 'name') {
        await columnRename(dataSource, table, data['rawValue'], value as string)
    }
}

